package com.zm.demo.doc.register;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.GetChildrenBuilder;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.List;

/**
 * @author z01140
 * @Package: com.zm.demo.doc.register
 * @Description:
 * @CreateDate 2022/10/18 11:14
 * @UpdateDate 2022/10/18 11:14
 */
public class ZookeeperApiInfoService {

    private static CuratorFramework curatorFramework;
    static {
        String connectString= "192.xxx.27.168:2181";
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(connectString)
                .sessionTimeoutMs(60000)
                .connectionTimeoutMs(15000)
                .namespace("dubbo")
                .retryPolicy(retryPolicy)
                .build();
        curatorFramework.start();
    }

    public static void main(String[] args) throws Exception {

        GetChildrenBuilder children = curatorFramework.getChildren();

        List<String> childrens = children.forPath("/");
        if (childrens == null){
            return;
        }

        childrens.stream().filter(url -> url.startsWith("com.xxx.nct")).forEach(url ->{
            try {
                children.forPath(url);
            } catch (Exception e) {
                e.printStackTrace();
            }
//        curatorFramework.getData().forPath();
        });
        System.out.println(childrens.size());

    }


    private Integer t(int i){
        if (i>10){
            return i;
        }else {
            return null;
        }
    }

    private Integer t1(){
        return t(19).intValue();
    }
}
